//
//  AIGirlLikesViewController.swift
//  AIGirl
//
//  Created by Rae on 2024/1/23.
//

import UIKit

class AIGirlLikesViewController: AIGirlBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("AIGirl_Profile_account_back").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color(.white).text("Likes").font(19.font_bold).alignment(.center).end()
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    private let emptyView = AIGirlEmptyView()
    private var pageIndex = 1
    private var dataSource: [AIGirlItemModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            titleLabel
            collectionView
            emptyView
        }
        
        backButton.leading(15).top(CGFloat.statusHeight + 5.0).size(30)
        titleLabel.centerHorizontally()
        titleLabel.CenterY == backButton.CenterY
        collectionView.top(.navi_height).leading(0).trailing(0).bottom(0)
        emptyView.top(.navi_height + 140.0).leading(0).trailing(0).height(150)
        emptyView.isHidden = true
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.itemSize = CGSize(width: (.screenWidth - 30.0)/2, height: 230.0)
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10.0, bottom: 0, right: 10.0)
        layout.sectionHeadersPinToVisibleBounds = true
        collectionView.style { v in
            v.collectionViewLayout = layout
            v.backgroundColor = .clear
            v.register(AIGirlDiscoverItem.self, forCellWithReuseIdentifier: AIGirlDiscoverItem.description())
            v.delegate = self
            v.dataSource = self
        }
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        
        collectionView.refresh_loadMore {
            self.loadMoreData()
        }.refresh_pull {
            self.loadFirstPage()
        }
        loadPageData()
    }
    
    func loadFirstPage() {
        pageIndex = 1
        loadPageData()
    }
    
    func loadMoreData() {
        pageIndex += 1
        loadPageData()
    }
    
    private func loadPageData(){
        
        self.request.start(.girlList(num: pageIndex, size: 20, startIndex: 0, type: 4), type: AIGirlListModel.self) { result in
            
            if result.isSuccessful, let model = result.model {
                if self.pageIndex == 1 {
                    self.dataSource.removeAll()
                }
                self.dataSource.append(contentsOf: model.rows)
                self.collectionView.reloadData()
                self.collectionView.refresh_end(model.rows.count < 20)
            }
        }
    }
}

extension AIGirlLikesViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        self.emptyView.isHidden = !(dataSource.count == 0 )
        return self.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let detailVC = AIGirlModelDetailViewController()
        detailVC.model = dataSource[indexPath.row]
        self.navigationController?.pushViewController(detailVC, animated: true)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let item = collectionView.dequeueReusableCell(withReuseIdentifier: AIGirlDiscoverItem.description(), for: indexPath) as? AIGirlDiscoverItem else { return UICollectionViewCell() }
        item.model = dataSource[indexPath.row]
        return item
    }
}
